//
//  CommodityRightAlert.swift
//  StarTimesTV
//
//  Created by 胡月明 on 2019/1/7.
//  Copyright © 2019年 StarTimes. All rights reserved.
//

import UIKit

class CommodityRightAlert: OLBaseViewController {

    var model:MemberRightsDto = MemberRightsDto()
    var channelArr:[ChannelDto] = []
    var program_list:[StarEpisodes] = []
    func reloadUI(){
   
        self.iconImage.backgroundColor = UIColor.white
        iconImage.layer.cornerRadius = scale_width(24)
        iconImage.layer.masksToBounds = true
        self.iconImage.st_setImage(with:model.image_url
            , placeholder:UIImage.init(resourceName:"ph_star_logo"))
        self.titleLabel.text = model.name
        
        let html = "<div style='font-family:Roboto-Regular'>" + model.description.replacingOccurrences(of: "&nbsp", with: " ") + "</div>"
        
        do{
            let srtData = html.data(using: String.Encoding.unicode, allowLossyConversion: true)!
            let strOptions = [NSAttributedString.DocumentReadingOptionKey.documentType:NSAttributedString.DocumentType.html]
            
            let attrStr = try NSAttributedString(data: srtData, options: strOptions, documentAttributes: nil)
            contentLabel.attributedText = attrStr
            contentLabel.sizeToFit()
            let size = contentLabel.sizeThatFits(CGSize(width: scale_width(280-20*2), height: 10000))
            let labelH = size.height

            if self.model.type_id == 1 {
                let width = scale_width(76)
                let left = margin18
                let margin = margin8
                
                let allRow = (channelArr.count) / 3
                
                let view = UIView.init()
                
                for (i,channelModel) in channelArr.enumerated(){
                    
                    let row = CGFloat(i / 3)
                    let col = CGFloat(i % 3)
              
                    let picX = left + (width + margin) * col;
                    
                    let picY = 0 + (width + margin) * row;
                    
                    let imageView: UIImageView = UIImageView.init(frame: CGRect(x: picX, y: picY, width: width, height: width))
                    imageView.tag = 100+i
                    imageView.st_setImage(with: channelModel.logoUrl)
                    let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(imageViewClick(sender:)))
                    imageView.addGestureRecognizer(singleTapGesture)
                    imageView.isUserInteractionEnabled = true
                    imageView.contentMode = .scaleAspectFill
                    imageView.clipsToBounds = true
                    imageView.layer.borderWidth = 1
                    imageView.layer.borderColor = UIColor(hex: 0xDDDDDD).cgColor
                    
                    view.addSubview(imageView)
                }
                
                contentBackView.addSubview(view)
                view.frame = CGRect(x: 0, y: labelH, width: scale_width(280), height: (width + margin) * CGFloat(allRow+1))
                
                scrollViewContentSizeView.snp.updateConstraints { (make) in
                    make.size.equalTo(CGSize(width: scale_width(280), height: labelH + view.frame.size.height))
                }
            }else if self.model.type_id == 9{
                
                let width = scale_width(120)
                let height = scale_width(160)
                let nameHeight = scale_width(40)
                let left = margin16
                let margin = margin8
                
                let allRow = (program_list.count) / 2
                
                let view = UIView.init()
                
                for (i,episodes) in program_list.enumerated(){
                    
                    let row = CGFloat(i / 2)
                    let col = CGFloat(i % 2)
                    
                    let picX = left + (width + margin) * col;
                    
                    let picY = 0 + (height + nameHeight + margin) * row;
                    
                    let imageView: UIImageView = UIImageView.init(frame: CGRect(x: picX, y: picY, width: width, height: height))
                    imageView.tag = 100+i
                    imageView.st_setImage(with: episodes.poster, placeholder: UIImage.tvs_videoPlaceholder_vertical)
                    let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(imageViewClick(sender:)))
                    imageView.addGestureRecognizer(singleTapGesture)
                    imageView.isUserInteractionEnabled = true
                    imageView.contentMode = .scaleAspectFill
                    imageView.clipsToBounds = true
                    imageView.layer.borderWidth = 1
                    imageView.layer.borderColor = UIColor(hex: 0xDDDDDD).cgColor
                    view.addSubview(imageView)
                    
                    let label = UILabel.init(frame: CGRect(x: picX, y: picY+height, width: width, height: nameHeight))
                    label.numberOfLines = 2
                    label.font = UIFont.medium(with: 13)
                    label.textColor = STColor.olBlack6
                    label.text = episodes.name
                    view.addSubview(label)
                }
                contentBackView.addSubview(view)
                view.frame = CGRect(x: 0, y: labelH, width: scale_width(280), height: (height + nameHeight + margin) * CGFloat(allRow+1))
                
                scrollViewContentSizeView.snp.updateConstraints { (make) in
                    make.size.equalTo(CGSize(width: scale_width(280), height: labelH + view.frame.size.height))
                }
                
                
            }

        }catch _ as NSError {
            
        }
    }
    
    @objc func imageViewClick(sender:UITapGestureRecognizer){
        self.dismiss(animated: true) {
            let imageView = sender.view
            
            if self.model.type_id == 1{
                let channelModel:ChannelDto = self.channelArr[((imageView?.tag)!-100)]
                STSRouter.openPlayerPage(channelId: "\(channelModel.id)",
                                         liveStatus: channelModel.liveStatus,
                                         videoName: channelModel.name)
            }else{
                let episodes:StarEpisodes = self.program_list[((imageView?.tag)!-100)]
                STSRouter.openPlayerPage(prgId: episodes.id,
                                        poster: episodes.poster)
            }

        }
    }
    
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        self.modalPresentationStyle = .custom
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func ViewClick() {
        self.dismiss(animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.addSubview(backView)
        
        let singleTapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewClick))
        backView.addGestureRecognizer(singleTapGesture)
  
        
        view.addSubview(whitebackView)
        whitebackView.addSubview(headerView)
        headerView.addSubview(imageview)
        headerView.addSubview(iconImage)
        headerView.addSubview(titleLabel)
        whitebackView.addSubview(contentBackView)
        contentBackView.addSubview(scrollViewContentSizeView)
        contentBackView.addSubview(contentLabel)
        whitebackView.addSubview(bottomview)
        bottomview.addSubview(okButton)
        
        backView.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalToSuperview()
        }
        
        whitebackView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.size.equalTo(CGSize(width: scale_width(280), height: scale_width(444)))
        }
        
        headerView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(scale_width(72))
        }
        
        imageview.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalToSuperview()
        }
        
        iconImage.snp.makeConstraints { (make) in
            let left = 12.ui_scale
            make.left.equalTo(left)
            make.size.equalTo(CGSize(width: 56.ui_scale, height: 56.ui_scale))
            make.centerY.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { (make) in
            
            make.left.equalTo(iconImage.snp.right).offset(10.ui_scale)
            make.right.equalToSuperview().offset(-scale_width(10.ui_scale))
            make.centerY.equalToSuperview()
        }
        
        contentBackView.snp.makeConstraints { (make) in
            make.top.equalTo(headerView.snp.bottom)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.width.equalTo(whitebackView)
        }
        
        bottomview.snp.makeConstraints { (make) in
            make.top.equalTo(contentBackView.snp.bottom)
            make.height.equalTo(scale_width(52))
            make.left.right.bottom.equalToSuperview()
        }
        
        scrollViewContentSizeView.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalToSuperview()
            make.size.equalTo(CGSize(width: scale_width(280), height: 0))
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview().inset(margin20)
            make.height.lessThanOrEqualTo(10000)
        }
        
        okButton.snp.makeConstraints { (make) in
            make.top.right.bottom.equalToSuperview()
            make.width.equalTo(scale_width(88))
        }
        
        reloadUI()
        
    }
    
    private lazy var backView:UIView = {
        let view = UIView.init()
        view.backgroundColor = UIColor.black
        view.alpha = 0.6
        return view
    }()
    private lazy var whitebackView:UIView = {
        let view = UIView.init()
        view.backgroundColor = UIColor.white
        return view
    }()
    
    private lazy var headerView: UIView = {
        let view = UIView.init()
        return view
    }()
    
    private lazy var imageview: UIImageView = {
        let imageview = UIImageView.init()
        imageview.image = "commodity_mb_alert_top".to_image
        imageview.contentMode = .scaleAspectFill
        imageview.isUserInteractionEnabled = true
        imageview.backgroundColor = UIColor.white
        return imageview
    }()
    
    private lazy var iconImage: UIImageView = {
        let image = UIImageView.init()
        image.contentMode = .scaleAspectFill
        image.clipsToBounds = true
        return image
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel.init()
        label.numberOfLines = 1
        label.textAlignment = .left
        label.font = UIFont.bold(with: fontSize16)
        label.textColor = UIColor.white
        return label
    }()
    
    private lazy var contentBackView:UIScrollView = {
        let scrollView = UIScrollView.init()
        return scrollView
    }()
    
    private lazy var scrollViewContentSizeView:UIView = {
        let view = UIView.init()
        return view
    }()
    
    private lazy var contentLabel:UILabel = {
        let titleLabel = UILabel.init()
        titleLabel.numberOfLines = 0
        return titleLabel
    }()
    
    private lazy var bottomview: UIView = {
        let view = UIView.init()
        view.backgroundColor = UIColor.white
        return view
    }()
    
    private lazy var okButton: UIButton = {
        let button = UIButton.init()
        button.setTitleColor("#775E33".to_color, for:.normal)
        button.setTitle(OLLocalize("ok"), for:.normal)
        button.titleLabel?.font = UIFont.bold(with: fontSize14)
        button.addTarget(self, action:#selector(didClickOKbutton), for:.touchUpInside)
        return button
    }()
    
    @objc func didClickOKbutton(){
        self.dismiss(animated: true, completion:nil)
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
